package junk;

import org.apache.commons.io.FileUtils;
import org.openqa.selenium.*;
import org.openqa.selenium.chrome.ChromeOptions;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.remote.RemoteWebDriver;
import org.openqa.selenium.support.PageFactory;
import org.testng.annotations.AfterClass;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;
import vo.LoginPage;

import java.io.File;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.text.Format;
import java.text.SimpleDateFormat;
import java.util.Date;

import static org.testng.Assert.assertEquals;

public class PO_OSChina_Login_chrome_headless1 {


    //osc_Login oscl = new osc_Login();

    LoginPage lp = new LoginPage();

    WebDriver wd;





    @BeforeTest
    public void beforeTest() throws IOException {

        DesiredCapabilities desiredCapabilities;
//        判断要打开的浏览器
        ChromeOptions chromeOptions = new ChromeOptions();
        // chromeOptions.addArguments("--headless");
        chromeOptions.addArguments("--headless","--disable-gpu","--window-size=1290,1080");

        desiredCapabilities = DesiredCapabilities.chrome();
        desiredCapabilities.setCapability("chromeOptions", chromeOptions);





//        拼接处要执行脚本的node 节点地址
        // String url = nodeURL + "/wd/hub";
        String url = "http://192.168.1.7:4444/wd/hub";
        wd = new RemoteWebDriver(new URL(url), desiredCapabilities);


    }

    

    @DataProvider(name = "login_data")
    public static Object[][] login_data() {
        return new Object[][]{

                {"James", "james9437974","用户名或密码错误123","hellotest1"},

                {"jamesz2011@126.com", "","密码不能为空","hellotest2"},

                {"", "james9437974","用户名不能为空","hellotest3"},

                {"", "","用户名不能为空","hellotest4"},

                {"jamesz2011@126.com", "james9437974","","hellotest5"},

        };
    }

    @Test(dataProvider = "login_data",dataProviderClass = PO_OSChina_Login_chrome_headless1.class)

    public void OSC_Login1(String Username, String Password,String exp,String hellotest) throws MalformedURLException {
 /*      // init_webdriver();
        System.out.println("my hellotest:----->"+"open");
        wd.get("http://www.qq.com");

        wd.get(lp.url);*/

        System.out.println("my hellotest:----->"+"open1");
        wd.get("http://www.oschina.net/");
        System.out.println("my hellotest:----->"+"open2");
        //wd.findElement(By.linkText("登录")).click();
        LoginPage login = PageFactory.initElements(wd, LoginPage.class);
        login.click_loginlink();
        login.click_passwordLoginTab();

       /* Actions action=new Actions(wdo333.wd);

        action.sendKeys(Keys.F5).perform();*/
       // wd.navigate().refresh();

       /* PlaySpeed();*/

      /*  Actions action = new Actions(wd);
        action.sendKeys(Keys.F5).perform();*/

        /* refresh();*/
      /*  login.input_username("janmesz2011@126.com");
        login.input_password("");*/


       login.input_login_data(Username,Password);

        // login.click_savebox();

        /* wdo33.wd.findElement(By.xpath(lp.savebox)).click();
*/
        WebElement savebox = wd.findElement(By.xpath(lp.savebox));
        boolean flag = savebox.isEnabled();
        boolean flag1 = true;

       /* System.out.println("flag:" + flag);*/
        //Assert.assertEquals(flag,flag1);

        login.click_savebox();

        //wdo33.PlaySpeed();

        login.click_loginbutton();




       /* WebElement note=wdo33.wd.findElement(By.xpath("//*[@id=\"account_login\"]/form/div/div[2]/div[2]/span"));
*/
        System.out.println("start-test");


       /* if (wdo33.isPresentEle(By.xpath("//*[@id=\"account_login\"]/form/div/div[2]/div[2]/span"), wdo33.wd)) {
            System.out.println("123456789");
            String noteinf=note.getText();

            //WebElement note1=wdo33.wd.findElement(By.xpath("//*[@id=\"account_login\"]/form/div/div[5]/div/span"));

            System.out.println("noteinf:"+noteinf);
            // Assert.assertEquals(noteinf,exp);
        }*/

        if (isPresentEle(By.xpath(login.NoPasswd),wd)){

            System.out.println("-------------");
            System.out.println("密码为空");
            System.out.println("-------------");
        }
        else if (isPresentEle(By.xpath(login.NoUsername),wd)){

            System.out.println("-------------");
            System.out.println("用户名为空");
            System.out.println("-------------");
        }
        else if (isPresentEle(By.xpath(login.NoUsername_Passwd),wd)){

            System.out.println("-------------");
            System.out.println("用户名、密码均为空");
            System.out.println("-------------");
        }


        else if (isPresentEle(By.xpath(login.Wrong_Username_Password),wd)){

            WebElement Wrong_Username_Password=wd.findElement(By.xpath(login.Wrong_Username_Password));
            String W_U_P=Wrong_Username_Password.getText();
            System.out.println("-------------");
            System.out.println("用户名或密码错误");
           if (W_U_P.equals(exp)){
               System.out.println("用户名或密码错误-----ok");
           }
           else {
               System.out.println("用户名或密码错误-----error");
               takeScreenshot(wd);
               takeScreenshot(wd);
           }

            System.out.println("-------------");
        }
        else if (isPresentEle(By.xpath(login.Rigit_Username_Password),wd)){
            System.out.println("-------------");
            System.out.println("用户名与密码正确");
            System.out.println("-------------");
            System.out.println("666");

            PlaySpeed();
        }



        PlaySpeed();

         // wdo33.wd.quit();

    }


    @AfterClass
    public void closebrowser() {
       test_wd();

    }

/*
  @Test(alwaysRun = true,dependsOnMethods = {"OSC_Login"})
    @Parameters("hello")
    public void endTest1(String hello){
       wdo33.test_wd();
        System.out.println("over"+hello);

    }*/




/*other*/




    public void PlaySpeed(){
        try {
            Thread.sleep(500);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }



    public void test_wd() {
        try {
            Thread.sleep(3600);
            wd.close();
            wd.quit();
            System.out.println("----close browser!!!-----");

        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println("wd ---ok");
    }

    /**
     * 是否存在某个节点
     *
     * @param by
     * @param wd
     * @return
     */
    public boolean isPresentEle(By by, WebDriver wd) {
        try {
            wd.findElement(by);
            return true;
        } catch (Exception e) {
            return false;
        }
    }


    /**
     * 获取当前时间，并转换为字符串
     * @return
     */
    private static String Date2String(){
        Format format = new SimpleDateFormat("yyyyMMddHHmmss");
        return format.format(new Date());
    }

    /**
     * 自动截屏
     * @param wd
     */
    public  void takeScreenshot(WebDriver wd){
        try {
            //获取截图file
            File srcFile= ((TakesScreenshot)wd).getScreenshotAs(OutputType.FILE);


            //新文件
            File targeFile = new File("pic/" + Date2String()+".png");

            //将图片移动到指定位置
            FileUtils.copyFile(srcFile, targeFile);
        }  catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * 获取断言失败的原因、信息和截屏
     * @param one
     * @param two
     * @param wd
     * @throws IOException
     * @throws InterruptedException
     */
    public  void assertEqu(String one,String two,WebDriver wd) throws InterruptedException, IOException{
        try{
            Thread.sleep(3000);
            takeScreenshot(wd);
            assertEquals(one, two);

        }catch(Exception e){
            System.out.println(e.getCause());
            System.out.println(e.getMessage());
        }
    }

/*other-end*/

}
